
********************************************************************************
** 	TITLE: SLCCU Analysis 
**
**	PURPOSE: Runs the do-files to create the outcome variables in both a wide and long data set format, perform analysis and outputs tables and figures.
**							
**  CREATED/EDITED BY: Kayla Wilding, Leah Kim
**
**	DATE CREATED: 6/15/2018
**
**	DATE LAST EDITED: 4/8/2018
********************************************************************************
/*

	Note: This do file runs 2 R scripts through shell command. 
	The syntax to run R script in a windows OS is -
		shell "C:/Program Files/R/R-X.Y.Z/bin/Rscript.exe" --vanilla "filepath/filename.R"
	- assuming the executable is at "C:/Program Files/R/R-X.Y.Z/bin/Rscript.exe"
	- For Mac OS the executable is generally at "/Library/Frameworks/R.framework/Resources/bin/R"
	- you also need to adjust the paths in the R scripts 

*/
/*******************************************************************************
1. Set globals
********************************************************************************/

	* please input your R-version here - 
	gl r_version "4.2.1"
	
	
	* Change root path
	if "`c(username)'" == "wjy5201" {
		gl root "C:/Users/wjy5201/GPRL Dropbox/Hasan Ahamed" // Change! 
	} 
	
	
	if "`c(username)'" == "ADD HERE" {
		gl root "ADD HERE" // Add the path here! 
	} 
	
	
	gl slccu "$root/CBL Replication Package"

	* Change path for subfolders
	gl outputtables "$slccu/04_tables/02_individual_tables"
	gl raw_data 	"$slccu/01_raw_data"
	gl dta 			"$slccu/03_dta/01_raw_dtas"
	gl adta 		"$slccu/03_dta/04_analysis_dtas"
	gl scripts 		"$slccu/01_do/02_tables"
	gl gph			"$slccu/05_graphs"
	gl ado 			"$slccu/00_ado"
	
	/* Below, indices in main tables */ 
	
	/* Financial risk taking scale */ 
	gl risk 				revbvrisk 		   /// Q18f: Take a risk for gain
	
	/* Self control and credit knowledge index */ 
	gl contknow				revbvafford 	///
							bvtoday 		///
							revbvlongterm   ///
							bvregret 		///
							bvtask 			///
							checkcs 		///
							creport 		///
							creditc 		///
							healthc 		///
							agec 			///
							borrowc 		///
							repayc	
							

	/* Liquidity index */ 						
	gl 	liqcf 				incgt30 		  /// // HH income greater than 30k
							bvstress 		  /// // financial situation source of stress--5=strongly disagree	
							bvbills 		  /// // difficult to cover bills--5= strongly disagree
							revbvneed 		  /// // could come up with $2k if need arose--5=strongly agree
							revfinsit 		  /// // overall financial situation--5-excellent
							savingsbal_mehb95 /// //savings balance 
							savslccu60more 	 // higher is more liquid
	
	/* Delinquency index */ 
	gl default_index_all	bin30acctsdl 	 ///
							bin90acctsdl 	 ///
							bincollect 		 ///
							binacctsderog 	 ///
							bintotamtpd 	 ///
							num30acctsdl 	 /// 
							num90acctsdl 	 ///
							numcollect 		 ///
							totamtpd 		 ///
							numacctsderog 
	
	gl default_index_f4		bin30acctsdl 	 ///
							bin90acctsdl 	 ///
							num30acctsdl 	 /// 
							num90acctsdl 	 
	
	gl default_index_l6		bincollect 		 ///
							binacctsderog 	 ///
							bintotamtpd 	 ///
							numcollect 		 ///
							totamtpd 		 ///
							numacctsderog 

	gl default_index_ser	bin90acctsdl 	 ///
							bincollect 		 ///
							binacctsderog 	 ///
							bintotamtpd 	 ///
							num90acctsdl 	 ///
							numcollect 		 ///
							totamtpd 		 ///
							numacctsderog 

	/* Installment credit activity index */	
	gl credac			ihs_openinstalltr1   /// number of open installment tradelines
						openinstallB1 		 /// 1 = any openinstallment loan
						ihs_inquiry121		//number of inquiries in last 12 months 
	
	/* Installment credit activity without the number of inquiries */
	gl credac2			ihs_openinstalltr1  ///
						openinstallB1 
						
	/* Revolving credit activity index */ 
	gl credac_rev 		ihs_openrevolvtr1  ///
						opentradeB1 	   ///
						ihs_usecreditoptrst1							
							
	/* New credit */ 
	gl newcredit			inquiry12 		///
							numaccts
								
	/* Amounts owed: balances index */
	gl amountsowed			agbalrevolvtrst	///
							agbalinstalltr	///
							agbalautoloan
							
	/* Amounts owed: utilization index */
	gl utilization			high_rev_util 	///
							openinstalltr 	///
							agbalrevolvbin	///
							agbalautobin	///
							agbalinstallnobin
	
	/* Credit mix */
	gl creditmix			bothtradelines ///
		
		
	/* Below, not in main tables but in causal forest */				
	
	/* Insecurity index */  				
	gl insecurity			revbvstress		   /// Q18a: Financial situation source of stress
							revbvbills 		   /// Q18d: Difficult to cover bills
							bvneed			   /// Q18e: Come up w $2k if need arose
							finsit			   /// Q17: How would you describe your overall financial situation?
							
	/* Self-control index */						
	gl selfcont				revbvafford		   /// Q18b: Carefully consider afford before buying
							bvtoday 		   /// Q18c: Live for today	
							revbvlongterm	   /// Q18g: Set long-term goals
							bvregret 	       /// Q18h: Wish better disciplined w money
							bvtask		       /// Q18j: Trouble finishing tasks					

	/* Attention to credit status */ 	
	gl credstatus 			checkcs		///
							creport		  
	
	/* Credit process knowledge */ 
	gl credknow 			creditc		///	
							healthc		///
							agec		///
							borrowc		///
							repayc 
	
	gl credstatusknow		checkcs		///
							creport		///
							creditc		///	
							healthc		///
							agec		///
							borrowc		///
							repayc 

	/* Lack of liquidity (used in forest) */ 						
	gl liquid				savslccu60less 	///
							incblmed		/// income below the median
							appdifficult	///  difficulty getting a loan approval
	
	
	/* SLCCU deposit index */
	gl crosssell			slccumem 	///
							bindepbal 	///
							depbal_me	
	
	/* Other index construction-related globals */ 
	
	gl indices_s 			z_insecurity_i     	///
							z_selfcont_i 		///
							z_risk_i 			///
							z_credstatus_i 		///
							z_credknow_i 		///
							z_liquid_i			///
							yg25				///
							z_credac_i			///	
							z_credstatusknow_i  ///
							z_credac2_i			/// -- without inquries
							z_credac_rev_i	 			
							
	gl indices_ac 			z_newcredit_i 		  ///
							z_amountsowed_i 	  ///
							z_utilization_i 	  ///
							z_default_index_all_i ///
							z_default_index_ser_i ///
							z_crosssell_i 		  ///
							z_contknow_i 		  ///
							z_liqcf_i
							
	gl htegroups 		htca ///	
						ltca ///
						mtca ///
						
	gl htegroups2 		htca2 ///	
						ltca2 ///
						mtca2 ///
	
	gl groupflags		flag_cblgroup		///
						flag_unintgroup		///
						flag_extragroup		///
						flag_randomized	    ///
						flag_randomized_scored_base ///
						flag_matched                ///
						flag_matched_scored_base    ///
						treatunint 			///
						extraunint
							
	gl baselineoutcomes	incgt30     ///
						revbvneed   ///
						revfinsit   ///
						savslccu60more ///
						inquiry121  ///
						numaccts1   ///
						tradelines1 ///
						agbalrevolvtrst1 ///
						agbalinstalltr1  ///
						agbalautoloan1   ///
						openinstallB1    ///
						openinstalltr1   ///
						opentradeB1      ///
						ihs_openinstalltr1 ///
						ihs_inquiry121     ///
						ihs_openrevolvtr1  ///
						ihs_usecreditoptrst1

	gl baselinechars	age        ///
						female 	   ///
						college    ///
						race_black ///
						married    ///
						child      ///
						adults 	
						
	gl revvars 			bvstress 	/// Q18a: Financial situation source of stress
						bvtoday 	/// Q18c: Live for today	
						bvbills 	/// Q18d: Difficult to cover bills
						bvrisk 		/// Q18f: Take a risk for gain
						bvregret 	/// Q18h: Wish better disciplined w money
						bvhquality 	/// Q18i: Demand highest quality
						bvtask 		/// Q18j: Trouble finishing tasks
						bvneed		///
						bvafford	///
						bvlongterm	///
						finsit

/*******************************************************************************
2. Install necessary programs
********************************************************************************/

cap ssc install ihstrans
cap ssc install winsor
cap ssc install revrs
cap ssc install outreg2
cap ssc install lassopack
cap ssc install pdslasso
cap ssc install unique
cap ssc install diff
cap net install st0085_2 
cap net install grc1leg

/*******************************************************************************
3. Run through all the do files
********************************************************************************/

clear all
set more off
set matsize 11000
version 17

*Run Scripts

run "${scripts}/01a Clean Sample and Surveys.do"
run "${scripts}/01b Create Outcomes.do"
run "${scripts}/01c Create Causal Forest Input Dataset.do"
run "${scripts}/02 Balance (T1, AT3a, AT3b).do"
run "${scripts}/03 Transition Matrix of Having a Credit Score (T2, RT2).do"
run "${scripts}/04 Main CBL TE (T3).do"
shell "C:/Program Files/R/R-"${r_version}"/bin/Rscript.exe" --vanilla "${slccu}/02_r/2020_03_11 Causal Forest Ficoscore numloans.R"
shell "C:/Program Files/R/R-"${r_version}"/bin/Rscript.exe" --vanilla "${slccu}/02_r/2020_03_11 Causal Forest scoredf numloans.R"
run "${scripts}/05a Causal Forest (T4).do"
run "${scripts}/05b Causal Forest Tercile (T5, Fig3).do"
run "${scripts}/06a Heterogeneity Driven by Credit Behaviors (T6, AT6).do"
run "${scripts}/06b Heterogeneity Driven by Credit Behaviors (T6 col1b2b).do"
run "${scripts}/07 Usage of Other SLCCU Products (T7).do"
run "${scripts}/08a Selection Effects (T8).do"
run "${scripts}/08b Selection Effects by Treatment and Timing (T9).do"
run "${scripts}/09 Calculate Mean of CATEs by Treatment (T10, AT8).do"
run "${scripts}/10 Randomization N (Fig2)"
run "${scripts}/11 External Validity (AT1).do"
run "${scripts}/12 Predict CBL Take-up Univariate (AT2).do"
run "${scripts}/13 Treatment Effects by Baseline Installment Borrowing (AT4, RLT1).do"
run "${scripts}/14 Causal Forest Tercile by Components (AT5a, AT5b).do"
run "${scripts}/15 Usage of other SLCCU products_winsorized outcomes (AT7).do"
run "${scripts}/16 Predictive Power (AT9).do"
run "${scripts}/17 ROC curves (AFig 1).do"
run "${scripts}/18 Response Letter 3_6"

*EOF* 



